var model = require('../../../model/bubbleSort.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isActive: '0',
    displayArrow: false,
    lowToHigh: false,
    saved_item:[],
    /**
     * 本地测试数据
     */
    item_list: [
      {
        id: 0, img: '../../../images/goods1.jpg', name: 'good1', price: 999999.99, post_fee: '包邮', sale_num: 299999, isSaved: false, upload_time: 20170914
      },
      {
        id: 1, img: '../../../images/goods1.jpg', name: 'good2', price: 999.99, post_fee: '包邮', sale_num: 99, isSaved: false, upload_time: 20170817
      },
      {
        id: 2, img: '../../../images/goods1.jpg', name: 'good3', price: 9.99, post_fee: '包邮', sale_num: 9, isSaved: true, upload_time: 20171017
      },
      {
        id: 3, img: '../../../images/goods1.jpg', name: 'good4', price: 99999.99, post_fee: '包邮', sale_num: 999, isSaved: false, upload_time: 20180117
      },
      {
        id: 4, img: '../../../images/goods1.jpg', name: 'good5', price: 0.9, post_fee: '包邮', sale_num: 2999, isSaved: false, upload_time: 20170917
      }
    ],
    sorted_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    var array = that.data.item_list;
    /**请求服务器得到产品列表 */
    //console.log(Object.keys(array[0])[5]);
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 选择产品排序规则
   */
  chooseOpt: function (e) {
    var that = this;
    var array = that.data.item_list;
    that.setData({
      isActive: e.currentTarget.id
    })
    if (that.data.isActive == '3') {
      that.setData({
        displayArrow: true
      })
      if (that.data.lowToHigh) {
        that.setData({
          lowToHigh: false
        })
      } else {
        that.setData({
          lowToHigh: true
        })
      }
    } else {
      that.setData({
        displayArrow: false
      })
    }
    //console.log(this.data.lowToHigh);
    var flag = that.data.lowToHigh;
    console.log(flag);

    //点击不同的按钮不同排序方式
    
    switch (that.data.isActive) {
      //综合
      case '0':
        //console.log(that.data.isActive);
        var arr = model.bubbleSort_lowToHigh(array, "id");
        console.log(arr);
        that.setData({
          item_list: arr
        })
        break;
      //销量
      case '1':
        //console.log(that.data.isActive);
        var arr = model.bubbleSort_highToLow(array, "sale_num");
        console.log(arr);
        that.setData({
          item_list:arr
        })
        break;
      //新品
      case '2':
        //console.log(that.data.isActive);
        var arr = model.bubbleSort_highToLow(array, "upload_time");
        console.log(arr);
        that.setData({
          item_list: arr
        })
        break;
      //价格
      case '3':
        //console.log(that.data.isActive);
        if (flag) {
          var arr = model.bubbleSort_lowToHigh(array, "price");
          console.log(arr);
          that.setData({
            item_list: arr
          })
        } else {
          var arr = model.bubbleSort_highToLow(array, "price");
          console.log(arr);
          that.setData({
            item_list: arr
          })
        }
        break;
    }
    //console.log(that.data.displayArrow);
    //console.log(that.data.isActive);
  },
  //收藏
  saveItem: function (e) {
    /**更新数据库与本地缓存 */
    var that = this;
    var arr = that.data.item_list;
    var currentItem = e.currentTarget.id;
    console.log(arr[currentItem].isSaved);
    if (arr[currentItem].isSaved == false) {
      arr[currentItem].isSaved = true;
      console.log(arr[currentItem].isSaved);
      that.setData({
        item_list: arr
      })
    }
    //console.log(e.currentTarget.id);
  },
  //取消收藏
  unsaveItem: function (e) {
    /**更新数据库与本地缓存 */
    var that = this;
    var arr = that.data.item_list;
    var currentItem = e.currentTarget.id;
    if (arr[currentItem].isSaved == true) {
      arr[currentItem].isSaved = false;
      that.setData({
        item_list: arr
      })
    }
  }
})